package com.wgts.wgts.mvp.view.activity.myorder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.CustomLineaLayoutManager;
import com.wgts.wgts.mvp.presenter.IEvaluateActivityPresenter;
import com.wgts.wgts.netmodle.resp.EvaluateEntity;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
 *
 *
 *   data:2018/9/2516:13
 *
 */public class EvaluateAdapter extends RecyclerView.Adapter<EvaluateAdapter.ViewHold> {
    private Context context;
    private List<EvaluateEntity> list;
    private EvaluateMeassageAdapter adapter;
    private CustomLineaLayoutManager lineaLayoutManager;
    private IEvaluateActivityPresenter presenter;
    private int selectPosition;

    public List<ViewHold> myViewHolderList = new ArrayList<>();

    public EvaluateAdapter(Context context, List<EvaluateEntity> list, IEvaluateActivityPresenter presenter) {
        this.context = context;
        this.list = list;
        this.presenter = presenter;
    }

    @NonNull
    @Override
    public ViewHold onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHold(LayoutInflater.from(context).inflate
                (R.layout.evaluate_recycle_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHold viewHold, final int i) {
        viewHold.evaluateRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
               presenter.ratingChange(i, (int)rating);
            }
        });
        MoHuGlide.getInstance().glideImg(context, list.get(i).productSmallImage, viewHold.shoppingCarItemImage);
        viewHold.shoppingCarTitle.setText(list.get(i).productName);
        viewHold.shoppingCarTypeShow.setText(list.get(i).productStandard);
        viewHold.shoppingCarPrice.setText("￥" + list.get(i).productPrice);
        lineaLayoutManager = new CustomLineaLayoutManager(context);
        lineaLayoutManager.setScrollEnabled(false);
        viewHold.evaluateMessageRecycle.setLayoutManager(lineaLayoutManager);
        adapter = new EvaluateMeassageAdapter(context, list.get(i).comments);
        viewHold.evaluateMessageRecycle.setAdapter(adapter);
        adapter.setOnItemClickListen(new EvaluateMeassageAdapter.onItemClickListen() {
            @Override
            public void onItemClick(View view, int position) {
                selectPosition = i;
                presenter.checkEvaluate(i, position);
            }
        });

        //holder绑定对应的position
        viewHold.setDataPosition(i);
        //判断list是否还有该holder， 没有就增加
        //因为list已经持有holder的引用，所有数据自动会改变
        if (!myViewHolderList.contains(viewHold)){
            myViewHolderList.add(viewHold);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHold extends RecyclerView.ViewHolder {
        @BindView(R.id.shopping_car_item_image)
        ImageView shoppingCarItemImage;
        @BindView(R.id.shopping_car_title)
        TextView shoppingCarTitle;
        @BindView(R.id.shopping_car_type_show)
        TextView shoppingCarTypeShow;
        @BindView(R.id.shopping_car_price)
        TextView shoppingCarPrice;
        @BindView(R.id.shopping_car_details)
        LinearLayout shoppingCarDetails;
        @BindView(R.id.evaluate_rating_bar)
        RatingBar evaluateRatingBar;
        @BindView(R.id.evaluate_message_recycle)
        RecyclerView evaluateMessageRecycle;
        private int position;

        private void setDataPosition(int position){
            this.position = position;
        }
        public ViewHold(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void checkNotify(){
        myViewHolderList.get(selectPosition).evaluateMessageRecycle.getAdapter().notifyDataSetChanged();
    }
}
